assert(Icetip, "没有找到Icetip!");

local Icetip = _G.Icetip
local Icetip_PowerBar = CreateFrame("Frame");
Icetip.PowerBar = Icetip_PowerBar;
local SM = LibStub("LibSharedMedia-3.0");

function Icetip_PowerBar:OnEnable(event, unit)
	self.db = Icetip.db["powerbar"];

	self:RegisterEvent("UNIT_MANA");
	self:RegisterEvent("UNIT_RAGE");
	self:RegisterEvent("UNIT_FOCUS");
	self:RegisterEvent("UNIT_ENERGY");
	self:RegisterEvent("UNIT_HAPPINESS");
	self:RegisterEvent("UNIT_RUNIC_POWER");
	self:RegisterEvent("UNIT_MAXMANA");
	self:RegisterEvent("UNIT_MAXRAGE");
	self:RegisterEvent("UNIT_MAXFOCUS");
	self:RegisterEvent("UNIT_MAXENERGY");
	self:RegisterEvent("UNIT_MAXHAPPINESS");
	self:RegisterEvent("UNIT_MAXRUNIC_POWER");
	self:RegisterEvent("UNIT_DISPLAYPOWER");

	self:SetBarPoint();
	if event == "UNIT_MANA" or event == "UNIT_RAGE" or event == "UNIT_FOCUS" or event == "UNIT_ENERGY" or event == "UNIT_HAPPINESS" or event == "UNIT_RUNIC_POWER" or event == "UNIT_MAXMANA" or event == "UNIT_MAXRAGE" or event =="UNIT_MAXFOCUS" or event == "UNIT_MAXENERGY" or event =="UNIT_MAXHAPPINESS" or event == "UNIT_MAXRUNIC_POWER" or event == "UNIT_DISPLAYPOWER" then
		self:UNIT_MANA(unit)
	end

	Icetip_PowerBar:TogglePowerbar(self.db.enable)
end

function Icetip_PowerBar:UNIT_MANA(unit)
	if not UnitIsUnit(unit, "mouseover") then
		return
	end
	self:Update()
end

function Icetip_PowerBar:SetUnitPowerBar()
	Icetip_PowerBar:TogglePowerbar(Icetip_PowerBar.db.enable)
end

function Icetip_PowerBar:SetBarPoint()
	if not powerbar then return end
	
	local position = self.db.position
	powerbar:SetWidth(0)
	powerbar:SetHeight(0)
	powerbar:ClearAllPoints()
	powerbar.side = position
	local healthbar = _G.Icetip_Health_Bar
	if position == "BOTTOM" then
		if healthbar and healthbar.side == "BOTTOM" then
			powerbar:SetPoint("TOPLEFT", healthbar, "BOTTOMLEFT");
			powerbar:SetPoint("TOPRIGHT", healthbar, "BOTTOMRIGHT");
		else
			powerbar:SetPoint("TOPLEFT", GameTooltip, "BOTTOMLEFT", 2, 0);
			powerbar:SetPoint("TOPRIGHT", GameTooltip, "BOTTOMRIGHT", -2, 0);
		end
		powerbar:SetHeight(self.db.size);
		powerbar:SetOrientation("HORIZONTAL");
	elseif position == "TOP" then
		powerbar:SetPoint("BOTTOMLEFT", GameTooltip, "TOPLEFT", 2, 0);
		powerbar:SetPoint("BOTTOMRIGHT", GameTooltip, "TOPRIGHT", -2, 0);
		powerbar:SetHeight(self.db.size);
		powerbar:SetOrientation("HORIZONTAL");
	end
end

function Icetip_PowerBar:OnTooltipShow()
	if not powerbar then
		powerbar = CreateFrame("StatusBar", "Icetip_Power_Bar", GameTooltip);
		powerbar:SetStatusBarTexture(SM:Fetch("statusbar", Icetip_PowerBar.db.texture));
		powerbar:SetMinMaxValues(0, 1);
		Icetip_PowerBar:SetBarPoint()
	end

	if not GameTooltip:GetUnit() then
		powerbar:Hide()
		return
	end

	Icetip_PowerBar:TogglePowerbar(Icetip_PowerBar.db.enable)
	Icetip_PowerBar:Update()
end

function Icetip_PowerBar:OnTooltipHide()
	if not powerbar then return end
	powerbar:Hide()
end

function Icetip_PowerBar:Update()
	if not powerbar then return end

	local powerType = UnitPowerType("mouseover");
	local maxpower = UnitPowerMax("mouseover");
	local power = UnitPower("mouseover");

	local value;
	if maxpower == 0 then
		value = 0
	else
		value = power/maxpower
	end
	powerbar:SetValue(value)

	if powerType == 0 then
		--mana
		powerbar:SetStatusBarColor(48/255, 113/255, 191/255);
	elseif powerType == 1 then
		--rage
		powerbar:SetStatusBarColor(226/255, 45/255, 75/255);
	elseif powerType == 2 then
		--focus
		powerbar:SetStatusBarColor(255/255, 210/255, 0);
	elseif powerType == 3 then
		--energy
		powerbar:SetStatusBarColor(1, 220/255, 25/255);
	elseif powerType == 4 then
		--happniess
		powerbar:SetStatusBarColor(0, 1, 1);
	elseif powerType == 6 then
		--runic power
		powerbar:SetStatusBarColor(0, 0.82, 1)
	end
end

function Icetip_PowerBar:TogglePowerbar(flag)
	if flag then
		Icetip_PowerBar:Update()
		if powerbar then
			powerbar:Show()
		end
	else
		if powerbar then
			powerbar:Hide()
		end
	end
end

Icetip_PowerBar:RegisterEvent("PLAYER_LOGIN");
Icetip_PowerBar:SetScript("OnEvent", Icetip_PowerBar.OnEnable)